package example.audio.track;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import example.audio.artist.Artist;

@Entity
@Table(name="tb_track")
@NamedQuery(name="tracksNoLongerThan",
		    query="from Track as track where track.playTime <= :length")
public class Track {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="track_id")
	private int id;
	@Column(nullable=false)
	private String title;
	@Column(name="file_path", nullable=false)
	private String filePath;
	@Column(name="play_time")
	@Temporal(TemporalType.TIME)
	private Date playTime;
	@Column
	@Temporal(TemporalType.DATE)
	private Date added;
	@Column
	@Temporal(TemporalType.DATE)
	private Date updated;
	@Column(nullable=false)
	private short volume;
	@ManyToMany
	@JoinTable(name="tb_track_artist",
			   joinColumns={@JoinColumn(name="artist_id")},
			   inverseJoinColumns={@JoinColumn(name="track_id")})
	private Set<Artist> artists;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public Date getPlayTime() {
		return playTime;
	}
	public void setPlayTime(Date playTime) {
		this.playTime = playTime;
	}
	public Date getAdded() {
		return added;
	}
	public void setAdded(Date added) {
		this.added = added;
	}
	public Date getUpdated() {
		return updated;
	}
	public void setUpdated(Date updated) {
		this.updated = updated;
	}
	public short getVolume() {
		return volume;
	}
	public void setVolume(short volume) {
		this.volume = volume;
	}
	public Set<Artist> getArtists() {
		return artists;
	}
	public void setArtists(Set<Artist> artists) {
		this.artists = artists;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Track other = (Track) obj;
		if (id != other.id)
			return false;
		return true;
	}
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Track [id=");
		builder.append(id);
		builder.append(", title=");
		builder.append(title);
		builder.append(", filePath=");
		builder.append(filePath);
		builder.append(", playTime=");
		builder.append(playTime);
		builder.append(", added=");
		builder.append(added);
		builder.append(", updated=");
		builder.append(updated);
		builder.append(", volume=");
		builder.append(volume);
		builder.append(", artists=");
		builder.append(artists);
		builder.append("]");
		return builder.toString();
	}
	
}
